<?php
defined('BASEPATH') or exit('No direct script access allowed');
require_once 'DB/MysqlModel.php';

class ChatRecord extends MysqlModel
{

    public $table = 'chat_record';

    public function __construct()
    {
        $this->db = $this->load->database('chat', true);
        parent::__construct();
        parent::setTable($this->table);
    }

    function getChatRecord($user)
    {
        $table = $this->table;
        $this->db->select('comment,sender,receiver,ctime,sender_name,receiver_name');
        $this->db->from($table);
        $this->db->where('sender', $user);
        $this->db->or_where('receiver', $user);
        $this->db->order_by('ctime desc');
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    function getHistoryUser($user)
    {
        $table = $this->table;
        $this->db->select('sender_name as username,sender');
        $this->db->from($table);
        $this->db->where('receiver', $user);
        $this->db->order_by('sender');
        $query = $this->db->get();
        $result = $query->result_array();
        foreach ($result as $k => $res) {
            $res['userImg'] = sprintf("http://u.thsi.cn/avatar/%d/%d.gif", $res['sender'] % 10000, $res['sender']);
            $result[$k] = $res;
        }
        return $result;
    }

    function getRecordForAnalisis()
    {
        $table = $this->table;
        $this->db->select('count(id) as count,comment,min(ctime) as ctime,max(category) as category');
        $this->db->from($table);
        $this->db->where("receiver='xxxxxxxxx'");
        $this->db->where("ctime>", strtotime(date('Y-m-d')));
        $this->db->where("ctime<", time());
        $this->db->group_by('comment');
        $this->db->order_by('count desc');
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    function searchQuestion($query)
    {
        $table = $this->table;
        $this->db->select('count(id) as count,comment,min(ctime) as ctime,max(category) as category');
        $this->db->from($table);
        if (! empty($query['question'])) {
            $this->db->like('comment', $query['question']);
        }
        if (! empty($query['category'])) {
            $this->db->where('category', $query['category']);
        }
        if (! empty($query['startTime'])) {
            $this->db->where('ctime >', strtotime($query['startTime']));
        }
        if (! empty($query['endTime'])) {
            $this->db->where('ctime <', strtotime($query['endTime']) + 86400);
        }
        $this->db->where("receiver='xxxxxxxxx'");
        $this->db->group_by('comment');
        $this->db->order_by('count desc');
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    function searchNotMatchQues($query)
    {
        $table = $this->table;
        $this->db->select('count(id) as count,comment,min(ctime) as ctime,max(category) as category, min(cate_group) as cate_group, min(is_add) as is_add, min(is_ignore) as is_ignore');
        $this->db->from($table);
        $this->db->like("bind_answer", "您的反馈内容我们已经收到，工作人员会在第一时间进行处理");
        if (! empty($query['question'])) {
            $this->db->like('comment', $query['question']);
        }
        if (! empty($query['category'])) {
            $this->db->where('category', $query['category']);
        }
        if (! empty($query['startTime'])) {
            $this->db->where('ctime >', strtotime($query['startTime']));
        }
        if (! empty($query['endTime'])) {
            $this->db->where('ctime <', strtotime($query['endTime']) + 86400);
        }
        if (! empty($query['cateGroup'])) {
            $this->db->where('cate_group', $query['cateGroup']);
        }
        if (! empty($query['dealState'])) {
            switch ($query['dealState']) {
                case 1:
                    $this->db->where('is_add', 0);
                    $this->db->where('is_ignore', 0);
                    break;
                case 2:
                    $this->db->where('is_add', 1);
                    break;
                case 3:
                    $this->db->where('is_ignore', 1);
                    break;
                default:
                    $this->db->where('is_add', 0);
                    $this->db->where('is_ignore', 0);
                break;
            }
        }
        $this->db->where("receiver='xxxxxxxxx'");
        $this->db->group_by('comment');
        $this->db->order_by('count desc');
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    function searchAllNotMatchQues($query)
    {
        $table = $this->table;
        $this->db->select('id,comment,cate_group');
        $this->db->from($table);
        $this->db->like("bind_answer", "您的反馈内容我们已经收到，工作人员会在第一时间进行处理");
        if (! empty($query['question'])) {
            $this->db->like('comment', $query['question']);
        }
        if (! empty($query['category'])) {
            $this->db->where('category', $query['category']);
        }
        if (! empty($query['startTime'])) {
            $this->db->where('ctime >', strtotime($query['startTime']));
        }
        if (! empty($query['endTime'])) {
            $this->db->where('ctime <', strtotime($query['endTime']) + 86400);
        }
        if (! empty($query['cateGroup'])) {
            $this->db->where('cate_group', $query['cateGroup']);
        }
        if (! empty($query['dealState'])) {
            switch ($query['dealState']) {
                case 1:
                    $this->db->where('is_add', 0);
                    break;
                case 2:
                    $this->db->where('is_add', 1);
                    break;
                case 3:
                    $this->db->where('is_ignore', 1);
                    break;
                default:
                    $this->db->where('is_add', 0);
                    $this->db->where('is_ignore', 0);
                break;
            }
        }
        $this->db->where("receiver='xxxxxxxxx'");
        $this->db->order_by('id asc');
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    function searchKnowledgePoint($query)
    {
        $table = $this->table;
        $this->db->select('count(id) as count,question_type as comment,min(ctime) as ctime,max(category) as category');
        $this->db->from($table);
        $this->db->not_like("bind_answer", "您的反馈内容我们已经收到，工作人员会在第一时间进行处理");
        $this->db->where("question_type !=", "");
        if (! empty($query['question'])) {
            $this->db->like('comment', $query['question']);
        }
        if (! empty($query['category'])) {
            $this->db->where('category', $query['category']);
        }
        if (! empty($query['startTime'])) {
            $this->db->where('ctime >', strtotime($query['startTime']));
        }
        if (! empty($query['endTime'])) {
            $this->db->where('ctime <', strtotime($query['endTime']) + 86400);
        }
        $this->db->where("receiver='xxxxxxxxx'");
        $this->db->group_by('question_type');
        $this->db->order_by('count desc');
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    function getNotMatchQues()
    {
        $table = $this->table;
        $this->db->select('count(id) as count,comment,min(ctime) as ctime,max(category) as category, min(cate_group) as cate_group, min(is_add) as is_add, min(is_ignore) as is_ignore');
        $this->db->from($table);
        $this->db->where("receiver='xxxxxxxxx'");
        $this->db->where("ctime>", strtotime(date('Y-m-d')));
        $this->db->where("ctime<", time());
        $this->db->like("bind_answer", "您的反馈内容我们已经收到，工作人员会在第一时间进行处理");
        $this->db->group_by('comment');
        $this->db->order_by('count desc');
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    function getAllNotMatchQues()
    {
        $table = $this->table;
        $this->db->select('id,comment,cate_group');
        $this->db->from($table);
        $this->db->where("receiver='xxxxxxxxx'");
        $this->db->where("ctime>", strtotime(date('Y-m-d')));
        $this->db->where("ctime<", time());
        $this->db->like("bind_answer", "您的反馈内容我们已经收到，工作人员会在第一时间进行处理");
        $this->db->order_by('id asc');
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    function getKnowledgePoint()
    {
        $table = $this->table;
        $this->db->select('count(id) as count,question_type as comment,min(ctime) as ctime,max(category) as category');
        $this->db->from($table);
        $this->db->where("receiver='xxxxxxxxx'");
        $this->db->where("ctime>", strtotime(date('Y-m-d')));
        $this->db->where("ctime<", time());
        $this->db->not_like("bind_answer", "您的反馈内容我们已经收到，工作人员会在第一时间进行处理");
        $this->db->where("question_type !=", "");
        $this->db->group_by('question_type');
        $this->db->order_by('count desc');
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    function getIDArrByComment($comment)
    {
        $table = $this->table;
        $this->db->select('id');
        $this->db->from($table);
        $this->db->like("bind_answer", "您的反馈内容我们已经收到，工作人员会在第一时间进行处理");
        $this->db->like('comment', $comment);
        if (! empty($_REQUEST['category'])) {
            $this->db->where('category', $_REQUEST['category']);
        }
        if (! empty($_REQUEST['startTime'])) {
            $this->db->where('ctime >', strtotime($_REQUEST['startTime']));
        }
        if (! empty($_REQUEST['endTime'])) {
            $this->db->where('ctime <', strtotime($_REQUEST['endTime']) + 86400);
        }
        if (! empty($_REQUEST['cateGroup'])) {
            $this->db->where('cate_group', $_REQUEST['cateGroup']);
        }
        if (! empty($_REQUEST['dealState'])) {
            switch ($_REQUEST['dealState']) {
                case 1:
                    $this->db->where('is_add', 0);
                    break;
                case 2:
                    $this->db->where('is_add', 1);
                    break;
                case 3:
                    $this->db->where('is_ignore', 1);
                    break;
                default:
                    break;
            }
        }
        $this->db->where("receiver='xxxxxxxxx'");
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }

    function updateSingleField($field, $value, $where)
    {
        $table = $this->table;
        $this->db->set($field, $value);
        $this->db->where($where);
        $result = $this->db->update($table);
        return $result;
    }

    function getAllNotMatchRecords($data)
    {
        $start = empty($data['startTime']) ? date('Y-m-d') : $data['startTime'];
        $end = empty($data['endTime']) ? date('Y-m-d H:i:s') : $data['endTime'];
        $table = $this->table;
        $this->db->select('*');
        $this->db->from($table);
        $this->db->like("bind_answer", "您的反馈内容我们已经收到，工作人员会在第一时间进行处理");
        $this->db->where('ctime >', strtotime($start));
        $this->db->where('ctime <', strtotime($end) + 86400);
        $this->db->where("receiver='xxxxxxxxx'");
        $this->db->where("cate_group != ''");
        $query = $this->db->get();
        $result = $query->result_array();
        return $result;
    }


}

